package in.iqing.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.control.util.UnitUtil;
import in.iqing.model.bean.Page;

/**
 * Created by neavo on 2015/1/8.
 */

public class ContentMenuDialog extends PopupWindow {

	@Bind(R.id.illustration_text)
	public TextView illustrationText;

	@Bind(R.id.progress_seek_bar)
	public SeekBar progressSeekBar;

	@Bind(R.id.progress_text)
	public TextView progressText;

	public Context ctx;
	public ICallback callback;

	public Integer index;
	public List<Page> pages;

	@SuppressLint("InflateParams")
	public static synchronized ContentMenuDialog newInstance(Context ctx, ICallback callback) {
		ContentMenuDialog dialog = new ContentMenuDialog();
		dialog.ctx = ctx;
		dialog.callback = callback;

		dialog.setFocusable(true);
		dialog.setOutsideTouchable(true);
		dialog.setAnimationStyle(R.style.dialog_style);
		dialog.setBackgroundDrawable(new ColorDrawable(ctx.getResources().getColor(R.color.transparent_dark_black)));

		dialog.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		dialog.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
		dialog.setContentView(LayoutInflater.from(ctx).inflate(R.layout.dialog_content_menu, null));

		return dialog;
	}

	@OnClick(R.id.setting_text)
	public void onSettingTextClick(View v) {
		callback.onSettingClick();
	}

	@OnClick(R.id.illustration_text)
	public void onIllustrationTextClick(View v) {
		if (pages.get(index).getType() == Page.TYPE_ILLUSTRATION) {
			callback.onIllustrationClick();
		}
	}

	private void setProgressSeekBar(int i) {
		progressText.setText((i + 1) + "/" + pages.size());
	}

	public void show(View parent, Integer index, List<Page> pages) {
		this.index = index;
		this.pages = pages;
		ButterKnife.bind(this, getContentView());

		setProgressSeekBar(index);
		progressSeekBar.setProgress(index);
		progressSeekBar.setMax(pages.size() - 1);
		progressSeekBar.setOnSeekBarChangeListener(new SeekBarOnSeekBarChangeListener());

		illustrationText.setTextColor(pages.get(index).getType() == Page.TYPE_ILLUSTRATION
						? ctx.getResources().getColor(R.color.primary_text)
						: ctx.getResources().getColor(R.color.tertiary_text)
		);

		update();
		showAtLocation(parent,
				Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
				0, UnitUtil.toPx(ctx, 20)
		);
	}

	public interface ICallback {

		void onSettingClick();

		void onIllustrationClick();

		void onProgressSeekBarChanged(int i);
	}

	private class SeekBarOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {

		@Override
		public void onStartTrackingTouch(SeekBar seek) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seek) {
			callback.onProgressSeekBarChanged(seek.getProgress());
		}

		@Override
		public void onProgressChanged(SeekBar seek, int progress, boolean fromUser) {
			setProgressSeekBar(progress);
		}
	}
}
